import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class mybatistest {
    private SqlSessionFactory SqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void init(){
        String resource = "mybatis-config.xml";
        Reader reader= null;
        try{
            reader = Resources.getResourceAsReader(resource);
           SqlSessionFactory sqlMapper = new
                   SqlSessionFactoryBuilder().build(reader);
           //创建sqlSession对象
           sqlSession= sqlMapper.openSession();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    @Test
    public void findAllStudentTest(){
       List<Student> list =
             sqlSession.selectList("com.itheima.mapper.StudentMapper.findAllStudent");
        for (Student student : list) {
            System.out.println(student);
        }
    }
    @After
    public void destory(){
        //提交事务
        sqlSession.commit();
        //关闭事物
        sqlSession.close();
    }
}
